//
//  YZQCityViewController.m
//  A_project2.0
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "YZQCityViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "YZQSecondMoneyCell.h"
#import "YZQSecondModel.h"

#import "OMGWebViewViewController.h"
@interface YZQCityViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *picArr;//轮播图数组
@property (nonatomic, strong) NSMutableArray *dataArr; //tableview数组
// 城市详情数组
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation YZQCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
#pragma mark - 创建tableview
    self.tableView =  [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;

    
    [self.view addSubview:_tableView];

    // 重用池
    [_tableView registerClass:[YZQSecondMoneyCell class] forCellReuseIdentifier:@"cell"];
    
    // 创建一个tableviewheader
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    _tableView.tableHeaderView = headView;

    
    // 获取数据
    [self data];
    
    // 地图
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-ditu"] style:UIBarButtonItemStylePlain target:self action:@selector(map)];

}
#pragma mark - 获取数据
- (void)data
{
    NSString *str =[NSString stringWithFormat:@"http:open.qyer.com/qyer/footprint/city_detail?city_id=%@",self.str];
    NSLog(@" str : %@",self.str);
    NSString *url =  @"&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88250754987822&lon=121.5399249782786&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1";
    [QPNetWorkingTool getNetWorkingTool:[NSString stringWithFormat:@"%@%@",str,url] dic:nil success:^(id result) {
        // 获取轮播图数据
        // 判断 如果轮播图 图片少于3的时候
        if ([result[@"data"][@"photos"] count] < 3) {
            UIImageView *backimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 200)];
            UILabel *lll = [[UILabel alloc]initWithFrame:CGRectMake(150, 20, self.view.frame.size.width - 150, 20)];
            lll.text = @"居然没图片";
            lll.textColor = [UIColor whiteColor];
            [backimg addSubview:lll];
            backimg.image = [UIImage imageNamed:@"boom.jpg"];
            _tableView.tableHeaderView = backimg;
            
            
        }else{
            self.picArr = result[@"data"][@"photos"];
            MyLoopsView *scroll = [MyLoopsView myLoopsViewWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 200) imageArr:_picArr];
            
#pragma mark - 获取tableview数据
            NSArray *arr = result[@"data"][@"new_discount"];
            self.dataArr = [YZQSecondModel modelHanderWithArray:arr];
            
            [_tableView reloadData];
            
            
            
#pragma mark - 轮播图上的label 中文 国家名
            UILabel *labelCnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 120, 50)];
            [scroll addSubview:labelCnname];
            
            labelCnname.text = result[@"data"][@"cnname"];
            labelCnname.font = [UIFont systemFontOfSize:18];
            labelCnname.textColor = [UIColor whiteColor];
            
            
#pragma mark -  轮播图上的label 英文 国家名
            UILabel *labelEnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 110, 120, 30)];
            [scroll addSubview:labelEnname];
            labelEnname.text = result[@"data"][@"enname"];
            labelEnname.textColor = [UIColor whiteColor];
            labelEnname.font = [UIFont systemFontOfSize:15];
            
            
#pragma mark -  轮播图上的label 国家简介
            UILabel *labelEntryCont = [[UILabel alloc] initWithFrame:CGRectMake(30, 130, self.view.frame.size.width - 60, 60)];
            labelEntryCont.text = result[@"data"][@"entryCont"];
            [scroll addSubview:labelEntryCont];
            labelEntryCont.font = [UIFont systemFontOfSize:14];
            labelEntryCont.numberOfLines = 2;
            labelEntryCont.textColor = [UIColor whiteColor];
            
            
            
            
            _tableView.tableHeaderView = scroll;
        }

    } fail:^(NSError *error) {
        
    }];
}
#pragma mark - tableview的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataArr.count;
    
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YZQSecondModel *m = _dataArr[indexPath.row];
    
    YZQSecondMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.titleLabel.text = m.title;
    cell.titleLabel.font = [UIFont systemFontOfSize:15];
    cell.titleLabel.numberOfLines = 0;
    cell.priceLabel.text = [[m.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    cell.priceoffLabel.text = m.priceoff;
    cell.priceoffLabel.font = [UIFont systemFontOfSize:15];
    cell.priceoffLabel.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    
    return cell;
    
    
    
}
#pragma mark -  tableview 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
#pragma mark - 自由行机票酒店往返跳转的webview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YZQSecondModel *m = _dataArr[indexPath.row];
OMGWebViewViewController *secondVC = [[OMGWebViewViewController alloc] init];
    secondVC.urlStr = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.8.3&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22",m.cid];
    
    
    [self.navigationController pushViewController:secondVC animated:YES];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
